package com.sew.sewweb.common.utils;

import org.apache.commons.codec.binary.Base64;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;

import java.io.*;

public class FileUtils {
    public static String base64Encode(byte[] bytes) {
        Base64 base64 = new Base64();
        return new String(base64.encode(bytes));
    }

    public static byte[] base64Decode(String base64Str) {
        Base64 base64 = new Base64();
        return base64.decode(base64Str);
    }

    public static byte[] getBytes(String filePath) {
        byte[] buffer = null;

        try {
            File file = new File(filePath);
            FileInputStream fis = new FileInputStream(file);
            ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
            byte[] b = new byte[1000];

            int n;
            while((n = fis.read(b)) != -1) {
                bos.write(b, 0, n);
            }

            fis.close();
            bos.close();
            buffer = bos.toByteArray();
        } catch (FileNotFoundException var7) {
            var7.printStackTrace();
        } catch (IOException var8) {
            var8.printStackTrace();
        }

        return buffer;
    }

    public static void createFile(byte[] bytes, String filePath, String fileName) throws Exception {
        BufferedOutputStream bos = null;
        FileOutputStream fos = null;

        try {
            File dir = new File(filePath);
            if (!dir.exists() && dir.isDirectory()) {
                dir.mkdirs();
            }

            File file = new File(filePath + File.separator + fileName);
            fos = new FileOutputStream(file);
            bos = new BufferedOutputStream(fos);
            bos.write(bytes);
        } catch (Exception var17) {
            var17.printStackTrace();
            throw new Exception("文件生成失败：" + var17.getMessage());
        } finally {
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException var16) {
                    var16.printStackTrace();
                }
            }

            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException var15) {
                    var15.printStackTrace();
                }
            }

        }

    }

    public static String getFileExt(String filename, Boolean withPoint) {
        if (!StringUtils.isEmpty(filename) && filename.lastIndexOf(46) != -1) {
            if (withPoint == null) {
                withPoint = true;
            }

            //int index = true;
            int index;
            if (withPoint) {
                index = filename.lastIndexOf(46);
            } else {
                index = filename.lastIndexOf(46) + 1;
            }

            String extName = filename.substring(index).toLowerCase();
            return extName;
        } else {
            return "";
        }
    }

    private FileUtils() {
    }

    public static void main(String[] args) {
        try {
            byte[] bytes = getBytes("D:/doc/设备原型问题列表.docx");
            String base64Code = base64Encode(bytes);
            byte[] newBytes = base64Decode(base64Code);
            createFile(newBytes, "D:/", "1.docx");
        } catch (Exception var4) {
            var4.printStackTrace();
        }

    }
}
